'''
Created on 2010-12-27

@author: ciid
'''
import pygame
from blocks import usable

class MapClass(object):
    use_void_block = False
    level_name = None
    matrix = []
    map_config = {}
    map = None
    width = 0
    # this is window height
    height = 600
    map_height = 0
    block_size= 50
    blocks_height = 0
    blocks_width = 0
    max_block_height = 0
    usable_blocks = {}
    loaded_blocks = {}
    
    
    def __init__(self, level_name = "l1"):
                
        #calculate max block height
        self.level_name = level_name
        self.max_block_height = self.height/self.block_size
        self.usable_blocks = usable.blocks
        
        #for developing !!!
        self.useOnlyVoidBlock(True);
        
        #loading level
        self.loadBlocks()
        self.loadLevel(level_name)
        self.createImageFromMatrix()
        
    def loadBlocks(self):
        if( self.use_void_block):
            self.loaded_blocks['void'] = pygame.image.load("blocks/"+self.usable_blocks['void']).convert_alpha()
        
    def getAllData(self):
        return {'img_path':"levels/compiled/"+self.level_name+".png",
                'matrix' : self.matrix,
                'config' : self.map_config,
                'block_size': self.block_size,
                }

    
    def getBlockImage(self, cell):
        if cell == 0:
            return False
        if self.use_void_block:
            return self.loaded_blocks['void']
    
    def loadLevel(self, level_name):
        '''Loading all important data'''
        import_string = "import levels."+level_name+" as level"
        exec import_string
        self.matrix = level.matrix
        self.map_config = level.config
        self.blocks_width = len(self.matrix[0])
        if len(self.matrix) < self.max_block_height:
            self.blocks_height = len(self.matrix)
        self.width = self.blocks_width*self.block_size
        self.map_height = self.map_config['height']

        
    def createImageFromMatrix(self):
        '''Creating image from matrix'''
        map = pygame.Surface((self.width, self.map_height*self.block_size));
        matrix = self.matrix
        matrix.reverse()
        try:
            map.fill(self.map_config['background-color'])
        except KeyError:
            pass
        tmp = matrix
        irow = 1;
        icell = 0;
        for row in tmp:
            icell = 0;
            for cell in row:
                img = self.getBlockImage(cell)
                if img == False:
                    icell +=1
                    continue
                cords = (icell*self.block_size, self.map_height*self.block_size-(irow*self.block_size))
                map.blit(img, cords)
                icell +=1
            irow +=1
        # saving compiled image
        pygame.image.save(map, 'levels/compiled/'+self.level_name+'.png')
    
    def useOnlyVoidBlock(self, bool):
        self.use_void_block = bool
        